package com.lg.problem;

import io.netty.util.concurrent.GlobalEventExecutor;
import io.netty.util.concurrent.Promise;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * Netty中的future 对于JDK中的Future 逻辑上实现的问题
 * https://www.jianshu.com/p/6a87ceb7f70a
 * <p>
 * Netty 4.1.49.Final
 *
 * @author by Mr. Li 2020/5/9 18:27
 */
public class NettyFuture {
    private final Promise defaultPromise = GlobalEventExecutor.INSTANCE.newPromise();
    private final ExecutorService executor = Executors.newFixedThreadPool(1);

    public static void main(String[] args) {

        NettyFuture main = new NettyFuture();

        main.isDoneTest(); // false

        main.isJDKDoneTest(); // true
    }

    private void isDoneTest() {

        defaultPromise.setUncancellable();

        defaultPromise.cancel(false);

        boolean isDone = defaultPromise.isDone();

        System.out.println(isDone);

    }

    private void isJDKDoneTest() {
        Future<?> task = executor.submit(() -> {
            return "Hello World";
        });

        task.cancel(true);

        boolean done = task.isDone();
        System.out.println(done);
        executor.shutdown();
    }
}
